package com.example.research.service;

import com.example.research.entity.Paper;
import com.example.research.mapper.PaperMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDate;
import java.util.List;

/**
 * 论文服务类
 */
@Service
@Transactional
public class PaperService {

    @Autowired
    private PaperMapper paperMapper;

    /**
     * 根据ID查询论文
     */
    public Paper findById(Long id) {
        return paperMapper.selectById(id);
    }

    /**
     * 查询所有论文
     */
    public List<Paper> findAll() {
        return paperMapper.selectAll();
    }

    /**
     * 根据用户ID查询论文
     */
    public List<Paper> findByUserId(Long userId) {
        return paperMapper.selectByUserId(userId);
    }

    /**
     * 根据审核状态查询论文
     */
    public List<Paper> findByAuditStatus(Integer auditStatus) {
        return paperMapper.selectByAuditStatus(auditStatus);
    }

    /**
     * 根据期刊查询论文
     */
    public List<Paper> findByJournal(String journalName) {
        return paperMapper.selectByJournal(journalName);
    }

    /**
     * 根据检索级别查询论文
     */
    public List<Paper> findByIndexLevel(String indexLevel) {
        return paperMapper.selectByIndexLevel(indexLevel);
    }

    /**
     * 根据作者查询论文
     */
    public List<Paper> findByAuthor(String author) {
        return paperMapper.selectByAuthor(author);
    }

    /**
     * 根据时间范围查询论文
     */
    public List<Paper> findByDateRange(LocalDate startDate, LocalDate endDate) {
        return paperMapper.selectByDateRange(startDate, endDate);
    }

    /**
     * 根据条件查询论文
     */
    public List<Paper> findByConditions(Long userId, String journalName, String indexLevel,
                                       Integer auditStatus, LocalDate startDate, LocalDate endDate) {
        return paperMapper.selectByConditions(userId, journalName, indexLevel, auditStatus, startDate, endDate);
    }

    /**
     * 创建论文
     */
    public Paper createPaper(Paper paper) {
        // 设置默认审核状态为待审核
        if (paper.getAuditStatus() == null) {
            paper.setAuditStatus(0);
        }

        paperMapper.insert(paper);
        return paper;
    }

    /**
     * 更新论文
     */
    public Paper updatePaper(Paper paper) {
        Paper existingPaper = paperMapper.selectById(paper.getId());
        if (existingPaper == null) {
            throw new RuntimeException("论文不存在");
        }

        // 检查是否可以修改
        if (!existingPaper.canModify()) {
            throw new RuntimeException("审核通过的论文不能修改");
        }

        // 检查权限：只能修改自己的论文
        if (!existingPaper.getUserId().equals(paper.getUserId())) {
            throw new RuntimeException("只能修改自己的论文");
        }

        paperMapper.update(paper);
        return paperMapper.selectById(paper.getId());
    }

    /**
     * 删除论文
     */
    public void deletePaper(Long id, Long userId) {
        Paper paper = paperMapper.selectById(id);
        if (paper == null) {
            throw new RuntimeException("论文不存在");
        }

        // 检查是否可以删除
        if (!paper.canModify()) {
            throw new RuntimeException("审核通过的论文不能删除");
        }

        // 检查权限：只能删除自己的论文
        if (!paper.getUserId().equals(userId)) {
            throw new RuntimeException("只能删除自己的论文");
        }

        paperMapper.deleteById(id);
    }

    /**
     * 更新审核状态
     */
    public void updateAuditStatus(Long id, Integer auditStatus, String auditComment) {
        Paper paper = paperMapper.selectById(id);
        if (paper == null) {
            throw new RuntimeException("论文不存在");
        }

        paperMapper.updateAuditStatus(id, auditStatus, auditComment);
    }

    /**
     * 提交审核
     */
    public void submitForAudit(Long id, Long userId) {
        Paper paper = paperMapper.selectById(id);
        if (paper == null) {
            throw new RuntimeException("论文不存在");
        }

        // 检查权限
        if (!paper.getUserId().equals(userId)) {
            throw new RuntimeException("只能提交自己的论文");
        }

        // 检查当前状态
        if (paper.getAuditStatus() == 1) {
            throw new RuntimeException("论文已审核通过，无需重复提交");
        }

        // 更新为待审核状态
        paperMapper.updateAuditStatus(id, 0, null);
    }
}
